package com.huilunyingshi.videoprovider.serviceImp;

import com.alibaba.dubbo.config.annotation.Service;
import com.huilunyingshi.videoapi.service.TTvSeriesService;
import com.huilunyingshi.videoprovider.dao.TLableMapper;
import com.huilunyingshi.videoprovider.dao.TTvSeriesMapper;
import com.huilunyingshi.videoprovider.entity.TLable;
import com.huilunyingshi.videoprovider.entity.TTvSeries;
import com.hullunyingshi.until.ControllerResult;
import com.hullunyingshi.until.PageHelper;
import com.hullunyingshi.until.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TvSeriesServiceImp implements TTvSeriesService {
    private final Logger logger = LoggerFactory.getLogger(AnimeServiceImp.class);

    @Autowired
    private TTvSeriesMapper TTvSeriesMapper;

    @Autowired
    private TLableMapper tLableMapper;

    /**
     * 查询所有电视剧
     * @param keyword
     * @param curPage
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getTvseriesList(String keyword, Integer curPage) {
        try {
            if (StringTools.strHasNoData(keyword)) {
                keyword = "";
            }
            if (StringTools.strHasNoData(curPage)) {
                curPage = 1;
            }
            Integer pageSize = 20;
            int totalCount = TTvSeriesMapper.getTvSeriesListCount();
            PageHelper pageHelper = new PageHelper(curPage, pageSize, totalCount);
            Integer totalPage = pageHelper.getTotalPage();
            Integer start = PageHelper.getStartIndex(curPage, pageSize);
            ArrayList<TTvSeries> tvSeriesList = TTvSeriesMapper.getTvseriesList(keyword, start, pageSize);

            pageHelper.setData(tvSeriesList);
            return new ControllerResult("200", "查询tv列表成功", pageHelper);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }
    }

    /**
     * 查询电视剧详情
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getTvseries(Long id) {
        try {
            if (StringTools.strHasNoData(id)){
                return new ControllerResult("403","电视剧id不能为空",null);
            }
            TTvSeries series = TTvSeriesMapper.selectByPrimaryKey(id);
            return new ControllerResult("200","点击TV显示页面成功",series);


        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }

    }

    /**
     * 分类查询电视剧
     * @param lid
     * @param curPage
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getTvseriesTypeList(Long lid, Integer curPage) {
        try {
            if (StringTools.strHasNoData(lid)){
                lid = 1l;
            }
            if (StringTools.strHasNoData(curPage)) {
                curPage = 1;
            }
            Integer pageSize =20;
            int totalCount = TTvSeriesMapper.getTvSeriesListCount();
            PageHelper pageHelper = new PageHelper(curPage, pageSize, totalCount);
            Integer startIndex = PageHelper.getStartIndex(curPage, pageSize);
            List<Map<String, Object>> AnimeTypeList = TTvSeriesMapper.getTvseriesTypeList(lid, startIndex, pageSize);
            pageHelper.setData(AnimeTypeList);


            return new ControllerResult("200", "查询typeTv列表成功", pageHelper);
        }catch(Exception e) {e.printStackTrace();
            throw new RuntimeException(e.toString());
        }


    }

    /**
     * 多条件查询电视剧
     * @param typeName
     * @param tvReleaseTime
     * @param tvArea
     * @param tvStatus
     * @param curPage
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getTvseriesTypeLists(String typeName, String tvReleaseTime,String tvArea,String tvStatus,Integer curPage) {
        try {

            if (StringTools.strHasNoData(curPage)) {
                curPage = 1;
            }
            Long lid = null;
            Long trtId = null;
            Long areaId = null;
            Long tsId = null;
            if (!StringTools.strHasNoData(typeName)) {
                TLable tLable = tLableMapper.selectByTypeName(typeName,2);
                lid = tLable.getId();
            }
            if (!StringTools.strHasNoData(tvReleaseTime)) {
                TLable tLable = tLableMapper.selectByTypeName(tvReleaseTime,2);
                trtId = tLable.getId();
            }
            if (!StringTools.strHasNoData(tvArea)) {
                TLable tLable = tLableMapper.selectByTypeName(tvArea,2);
                areaId = tLable.getId();
            }
            if (!StringTools.strHasNoData(tvStatus)) {
                TLable tLable = tLableMapper.selectByTypeName(tvStatus,2);
                tsId = tLable.getId();
            }

            Integer pageSize =20;
            int totalCount = TTvSeriesMapper.getTvSeriesListCount();
            PageHelper pageHelper = new PageHelper(curPage, pageSize, totalCount);
            Integer startIndex = PageHelper.getStartIndex(curPage, pageSize);
            List<Map<String, Object>> TTvTypeLists = TTvSeriesMapper.getTvseriesTypeLists(lid,trtId,areaId,tsId,startIndex,pageSize);

            pageHelper.setData(TTvTypeLists);


            return new ControllerResult("200", "查询typeTv列表成功", pageHelper);
        }catch(Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }
    }
}
